package product;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

import javax.swing.SwingConstants;

public class CodeTableAction extends JFrame {

	HashMap<String, CodeData> cod;
	Vector<String> vec;
	int index;
	DefaultTableModel model;
	DecimalFormat df = new DecimalFormat("###,###,###.##");
	
	private JPanel contentPane;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JTextField proCode;
	private JTextField proName;
	private JTextField proPrice;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JLabel status;

	
	/**
	 * Create the frame.
	 */
	public CodeTableAction(HashMap<String, CodeData> c,Vector<String> v,
			DefaultTableModel m,int i) {
		this.cod = c;
		this.vec = v;
		this.index = i;
		this.model=m;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 225, 180);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblNewLabel());
		contentPane.add(getLblNewLabel_1());
		contentPane.add(getLblNewLabel_2());
		contentPane.add(getProCode());
		contentPane.add(getProName());
		contentPane.add(getProPrice());
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());
				
		proCode.setText(vec.get(0));
		proName.setText(vec.get(1));
		proPrice.setText(vec.get(2));
		contentPane.add(getStatus());
		
	}
	
	public String replace(String s) {
		String temp = s.replace(",","");
		String temp2 = temp.replace("원","");
		return temp2;
	}
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("제품코드");
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("제품명");
			lblNewLabel_1.setBounds(12, 35, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("단가");
			lblNewLabel_2.setBounds(12, 60, 57, 15);
		}
		return lblNewLabel_2;
	}
	private JTextField getProCode() {
		if (proCode == null) {
			proCode = new JTextField();
			proCode.setEditable(false);
			proCode.setBounds(81, 7, 116, 21);
			proCode.setColumns(10);
		}
		return proCode;
	}
	private JTextField getProName() {
		if (proName == null) {
			proName = new JTextField();
			proName.setBounds(81, 32, 116, 21);
			proName.setColumns(10);
		}
		return proName;
	}
	private JTextField getProPrice() {
		if (proPrice == null) {
			proPrice = new JTextField();
			proPrice.setHorizontalAlignment(SwingConstants.RIGHT);
			proPrice.setBounds(81, 57, 116, 21);
			proPrice.setColumns(10);
		}
		return proPrice;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("수정");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String code = proCode.getText();
					String name = proName.getText();
					int price = Integer.parseInt(replace(proPrice.getText()));
					CodeData data = new CodeData(code, name, price);
					cod.put(code, data); //저장
					try {
						FileOutputStream fw = new FileOutputStream("CodeList.dat");
						ObjectOutputStream bw = new ObjectOutputStream(fw);
						bw.writeObject(cod);
						bw.close();
						fw.close();
						System.out.println("수정성공");
					} catch (Exception e) {
						// TODO: handle exception
						System.out.println("error");
					}
					status.setText(code + "수정됨");

					String str = df.format((double)price);
					model.setValueAt(name, index, 1);
					model.setValueAt(String.valueOf(str)+"원", index, 2);// 변경할 내용, 행, 열
					
				
				}
			});
			btnNewButton.setBounds(12, 85, 84, 23);
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("삭제");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int yn = JOptionPane.showConfirmDialog(null, "삭제하시겠습니까?");
					if (yn == JOptionPane.NO_OPTION)
						return;
					else if (yn == JOptionPane.YES_NO_OPTION) {

						String code = proCode.getText();
						cod.remove(code); // 삭제
						try {
							FileOutputStream fw = new FileOutputStream(
									"CodeList.dat");
							ObjectOutputStream bw = new ObjectOutputStream(fw);
							bw.writeObject(cod);
							bw.close();
							fw.close();
							System.out.println("삭제성공");
							model.removeRow(index);//테이블에서 삭제
							dispose();// 수정&삭제창 닫기
						} catch (Exception e) {
							System.out.println("error");
						}
					}
				}
			});
			btnNewButton_1.setBounds(108, 85, 89, 23);
		}
		return btnNewButton_1;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel(" ");
			status.setOpaque(true);
			status.setBackground(Color.CYAN);
			status.setBounds(12, 118, 185, 15);
		}
		return status;
	}
}
